package com.lagou.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lagou.entity.ResponseDTO;
import com.lagou.entity.User;
import com.lagou.entity.UserResponse;
import com.lagou.service.UserService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @GetMapping("/login")
    public ResponseDTO<User> login(String phone, String password) {

        ResponseDTO<User> responseDTO = new ResponseDTO();
        User user = userService.login(phone, password);
        if (user != null) {

            responseDTO.setState(200);
            responseDTO.setMessage("登录 成功");
        }else{

            responseDTO.setState(300);
            responseDTO.setMessage("登录 失败");
        }

        responseDTO.setContent(user);
        return responseDTO;
    }

    @PostMapping("updateUserInfo")
    public UserResponse updateUserInfo(@RequestBody User user) {

        User userParam=new User();
        userParam.setId(user.getId());
        userParam.setPortrait(user.getPortrait());
        userParam.setName(user.getName());

        try {
            userService.update(userParam);
            return new UserResponse(true, 200, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new UserResponse(false, 300, "修改失败");
        }
    }

    @PostMapping("updatePassword")
    public UserResponse updatePassword(@RequestBody User user) {

        User user1 = new User();
        user1.setId(user.getId());
        user1.setPassword(user.getPassword());
        userService.update(user1);
        try {
            userService.update(user1);
            return new UserResponse(true, 200, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new UserResponse(false, 300, "修改失败");
        }
    }
}
